<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 17-2-21
 * Time: 下午5:08
 */

namespace Cframe\Tpllib;

use Cframe\Log;

class TplFunctions extends Tpl {
    public function tagLoad($tag,$end){
        $file = isset($tag['file'])?$tag['file']:'';
        $str = '';
        $arr = explode(',',$file);
        foreach ($arr as $val){
            $ext = pathinfo($val)["extension"];
            switch ($ext){
                case "js":
                    $str .= "<script type='text/javascript' src='".trim($val)."'></script>";
                    break;
                case "css":
                    $str .= "<link rel='stylesheet' type='text/css' href='".trim($val)."'/>";
                    break;
                case 'php':
                    $str .= "<?php include '".trim($val)."';?>";
                    break;
            }
        }
        return $str;
    }

    public function tagInclude($tag,$end){
        $file = isset($tag['file'])?$tag['file']:'';
        $path = CON_PATH . MODULE_NAME . DS . BIND_VIEW . DS;
        if (strpos($file,':') !== false){
            $file = explode(":",$file);
            $path = $path . current($file);
            $file = end($file);
        }
        if (is_dir($path)){
            $filename = $this->convertTemp($file,$path);
        }
        $str = "<?php ";
        $str .= "include '$filename';";
        $str .= " ?>";
        return $str;
    }

    public function tagUrl($tag,$end){
        $domain = isset($tag['domain'])?$tag['domain']:'';
        $path = isset($tag['path'])?$tag['path']:'';
        $url = new \Cframe\Url();
        $str = $url->CreateUrl($domain) . $path;
        unset($url);
        return $str;
    }

    public function tagIf($tag,$end){
        $condition = isset($tag['condition'])?$tag['condition']:false;
        $str = "<?php if ({$condition}): ?>";
        $str .= $end;
        $str .= "<?php endif; ?>";
        return $str;
    }

    public function tagElse($tag,$end){
        $str = "<?php else: ?>";
        return $str;
    }

    public function tagElseif($tag,$end){
        $condition = isset($tag['condition'])?$tag['condition']:false;
        $str = "<?php elseif({$condition}): ?>";
        return $str;
    }

    public function tagCif($tag,$end){
        $name = isset($tag['name'])?$tag['name']:'';
        $var = isset($tag['var'])?$tag['var']:'';
        $str = "<?php ";
        $str .= "if (isset(\${$var}['{$name}'])):";
        $str .= "?>";
        $str .= $end;
        $str .= "<?php ";
        $str .= "endif;";
        $str .= "?>";
        return $str;
    }

    public function tagCvif($tag){
        $name = isset($tag['name'])?$tag['name']:'';
        $var = isset($tag['var'])?$tag['var']:'';
        $df = isset($tag['default'])?$tag['default']:'';
        $str = "<?php ";
        $str .= "if (isset(\${$var}['{$name}'])):";
        $str .= "echo \${$var}['{$name}'];";
        $str .= "else:";
        $str .= "echo '{$df}';";
        $str .= "endif;";
        $str .= "?>";
        return $str;
    }

    public function tagLoop($tag,$end){
        $name = isset($tag['name'])?$tag['name']:'';
        $id = isset($tag['id'])?$tag['id']:'';
        $num = isset($tag['num'])?$tag['num']:'';
        $str = "<?php ";
        if (!empty($num)){
            $str .= "\${$num} = 0;";
        }
        $str .= "if (isset(\${$name}) && is_array(\${$name})):foreach (\${$name} as \${$id}): ?>";
        $str .= $end;
        $str .= "<?php ";
         if (!empty($num)){
            $str .= "\${$num} = \${$num}+1;";
        }
        $str .= "endforeach;endif; ?>";
        return $str;

    }
}